package cn.mymall.web.controller.houtai;

import cn.mymall.constant.EMallConstant;
import cn.mymall.redis.RedisHelper;
import cn.mymall.util.BaseWebUtil;
import cn.mymall.vo.user.UserResVo;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseHoutaiController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected RedisHelper redisHelper;
    @Autowired
    protected JdbcTemplate jdbcTemplate;
    protected final HttpServletResponse getResponse(){
        ServletRequestAttributes holder = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(holder!=null){
            HttpServletResponse response = holder.getResponse();
            if(response!=null){
                return response;
            }
        }
        return null;
    }

    /**
     * 获取当前请求的request对象
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes holder = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (holder == null) return null;
        HttpServletRequest request = holder.getRequest();
        return request;
    }

    protected UserResVo getLoginUser() {
        HttpServletRequest request = BaseWebUtil.getRequest();
        return (UserResVo) request.getAttribute(EMallConstant.REQUEST_LOGIN_USER);
    }

    protected boolean checkParamsIsBlank(Object ...params){
        if(ArrayUtils.isEmpty(params)) return true;
        for(Object object : params){
            if(object == null) return true;
            else if(object instanceof String && StringUtils.isBlank((String)object)){
                return true;
            }
        }
        return false;
    }
}
